package com.lemon.tools;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

/**
 * @author lemonjesnme
 * @category is used as save local user send to server's image. this image will
 *           save as a jpg files the path is this tomcat project path
 */
public class Files {
	/**
	 * @category the project path is not be create ,this method can create the
	 *           file path and dir
	 * @param path
	 * @return
	 */
	public static boolean createFileDir(String path) {
		File dirFile = null;
		try {
			dirFile = new File(path);
			if (!(dirFile.exists()) && !(dirFile.isDirectory())) {
				boolean creadok = dirFile.mkdirs();
				if (creadok) {
					System.out.println(" ok ");
				} else {
					System.out.println(" err ");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e);
			return false;
		}
		return true;
	}

	/**
	 * @category save the file from jsp inputstream
	 * @param in
	 * @param destFile
	 * @return
	 */
	public static boolean saveFile(InputStream in, String destFile) {
		File dest = new File(destFile);
		if (!dest.exists()) {
			try {
				dest.createNewFile();
			} catch (IOException e) {
				System.out.println("IOException");
			}
		}
		OutputStream out = null;
		BufferedInputStream inb = null;
		BufferedOutputStream oub = null;
		try {
			out = new FileOutputStream(dest);
			inb = new BufferedInputStream(in);
			oub = new BufferedOutputStream(out);

			byte[] read = new byte[1024];
			int len = in.read(read);

			while (len != -1) {
				oub.write(read, 0, len);
				len = in.read(read);
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			try {
				oub.close();
				inb.close();
				out.close();
				in.close();
			} catch (Exception e0) {
				e0.printStackTrace();
				return false;
			}
		}
		CompressImage ci = CompressImage.getImageCompress();
		File dested = new File(Constants.tomcatSrcPath + "100000.jpg");
		ci.compress(dest, dested);
		renameFile(Constants.tomcatSrcPath + "100000.jpg", destFile);
		return true;
	}

	public static boolean saveFile1(InputStream in, String destFile) {
		File dest = new File(destFile);
		if (!dest.exists()) {
			try {
				dest.createNewFile();
			} catch (IOException e) {
				System.out.println("IOException");
			}
		}
		OutputStream out = null;
		BufferedInputStream inb = null;
		BufferedOutputStream oub = null;
		try {
			out = new FileOutputStream(dest);
			inb = new BufferedInputStream(in);
			oub = new BufferedOutputStream(out);

			byte[] read = new byte[1024];
			int len = in.read(read);
			len = len - 512;
			while (len != -1) {
				oub.write(read, 0, len);
				len = in.read(read);
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			try {
				oub.close();
				inb.close();
				out.close();
				in.close();
			} catch (Exception e0) {
				e0.printStackTrace();
				return false;
			}
		}
		CompressImage ci = CompressImage.getImageCompress();
		File dested = new File(Constants.tomcatSrcPath + "100000.jpg");
		ci.compress(dest, dested);
		renameFile(Constants.tomcatSrcPath + "100000.jpg", destFile);
		return true;
	}
	
	
	public static void renameFile(String oldFullName, String newFullName) {
		// File sdcard = Environment.getExternalStorageDirectory();
		File from = new File(oldFullName);
		File to = new File(newFullName);
		from.renameTo(to);
	}

	public static void copyFile(String oldFullName, String newFullName)
			throws Exception {
		File f1 = new File(oldFullName);
		File f2 = new File(newFullName);
		int length = 2097152;
		FileInputStream in = new FileInputStream(f1);
		FileOutputStream out = new FileOutputStream(f2);
		byte[] buffer = new byte[length];
		while (true) {
			int ins = in.read(buffer);
			if (ins == -1) {
				in.close();
				out.flush();
				out.close();
			} else
				out.write(buffer, 0, ins);
		}
	}

}
